﻿using duia.Canteenpay.Application.AppService;
using duia.Canteenpay.Domain;
using duia.Canteenpay.Domain.IRepository;
using duia.Canteenpay.ViewModel.Users;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace duia.Canteenpay.WebApi.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };

        private readonly ILogger<WeatherForecastController> _logger;

        private readonly IAppUnitOfWork _appUnitOfWork;
        private readonly IUserRepository _userRepository;
        private readonly IDishesCategoryRepository _dishesCategoryRepository;

        private readonly UsersAppService _usersAppService;


        public WeatherForecastController(ILogger<WeatherForecastController> logger, IAppUnitOfWork appUnitOfWork, IUserRepository userRepository,
            IDishesCategoryRepository dishesCategoryRepository,
            UsersAppService usersAppService)
        {
            _logger = logger;
            _appUnitOfWork = appUnitOfWork;
            _userRepository = userRepository;
            _dishesCategoryRepository = dishesCategoryRepository;
            _usersAppService = usersAppService;
        }

        [HttpGet]
        public async Task<IEnumerable<WeatherForecast>> Get()
        {
            try
            {
                for (int i = 0; i < 1; i++)
                {
                    await _usersAppService.AddAsync(new AddUsersModelReceive()
                    {
                        Account = "admin" + i,
                        UserName = "admin" + i,
                        UserPassWord = "admin" + i

                    });
                }


                //var dishesCategory = new DishesCategory("素菜");
                //dishesCategory.AddDishes("牛油白菜", "Cs1002", 50, "");
                //_dishesCategoryRepository.AddAsync(dishesCategory);

                ////提交保存
                //await _dishesCategoryRepository.AppUnitOfWork.SaveChangesAsync();



                var find = _dishesCategoryRepository.Find(x => x.KeyId == Guid.Parse("1054a953-6538-4d22-b5b0-ed5ec19bdefe")).Result;
                var findDishes = find.Dishes.ToList();
                for (int i = 0; i < 10; i++)
                {
                    find.Dishes.Remove(findDishes[i]);
                }
                _dishesCategoryRepository.UpdateAsync(find);
                var task = await _userRepository.Find(x => x.UserName.Contains("admin"));
                task.SetUserPassWord("123");
                _userRepository.UpdateAsync(task);

                await _dishesCategoryRepository.AppUnitOfWork.SaveChangesAsync();

                //var find = _userRepository.Find(x => true).Result;

                //var disheses = _appUnitOfWork.Dishes.ToList();
                //var DishesCategory = _appUnitOfWork.DishesCategory.ToList();
            }
            catch (Exception e)
            {

            }

            var rng = new Random();
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = rng.Next(-20, 55),
                Summary = Summaries[rng.Next(Summaries.Length)]
            })
            .ToArray();
        }
    }
}
